<?php

include_once dirname(__FILE__) . "/error.php";
include_once dirname(__FILE__) . "/log.php";
include_once dirname(__FILE__) . "/conf.php";

class Open{

    private $open_id = false;
    private $openkey = false;
    private $sig = false;
    private $pf = false;
    private $url = false;

    public function init()
    {
        global $err_code;
        
        $this->url =  (string)Conf::get("openqq.url");
        if($this->url == false)
        {
            $err_code = ec_conf_error;
            Log::error("openqq.url conf error");
            return false;
        }
        Log::debug("openqq.url = $this->url");
        return true;
    }
    
    private function send_req($arg)
    {
        $data = json_encode($arg);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }

    public function get_info($arg)
    {
        global $err_code;
        
        if(!is_array($arg) || !array_key_exists("open_id", $arg) ||
           !array_key_exists("openkey", $arg) || !array_key_exists("sig", $arg) ||
           !array_key_exists("pf", $arg) )
        {
            $err_code = ec_param_error;
            Log::error("open arg error");
            return false;
        }
        $this->open_id = $arg['open_id'];
        $this->openkey = $arg['openkey'];
        $this->sig = $arg['sig'];
        $this->pf = $arg['pf'];
        
        $req = array(
            "open_id" => $this->open_id, 
            "openkey" => $this->openkey, 
            "sig" => $this->sig, 
            "pf" => $this->pf,
            "interface" => "get_info", 
        );
        $ret = $this->send_req($req);
        if($ret == false)
        {
            $err_code = ec_open_qq_error;
            Log::error("open get_info failed");
        }
        else
        {
            Log::debug("get_info ret:" . $ret);
            $ret = json_decode($ret, true);
        }
        return $ret;
    }
    
};

?>
